package com.xxhui.rxjavademo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;

public class MainActivity extends AppCompatActivity {

    private static final String TAG=MainActivity.class.getSimpleName();
    private TextView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        view = (TextView) findViewById(R.id.show_text);

        //被观察者   Observable:可观测的
        Observable<String> stringObservable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("hello");
                subscriber.onNext("world");
                subscriber.onCompleted();
            }
        });
        Observable<String> justObservable = Observable.just("hello world!");

        //观察者  Observer:观察者
        Observer<String> stringObserver = new Observer<String>() {
            @Override
            public void onCompleted() {
                Log.w(TAG, "onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.w(TAG, "onError");
            }

            @Override
            public void onNext(String s) {
                view.append("\nstringObserver:"+s);
                Log.w(TAG, "onNext:"+s);
            }
        };

        Subscriber<String> stringSubscriber = new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                view.append("\nstringSubscriber:"+s);
                System.out.println(s);
            }
        };

        Action1<String> onNextAction = new Action1<String>() {
            @Override
            public void call(String s) {
                view.append("\nonNextAction:"+s);
            }
        };

        //订阅   subscribe:订阅
        stringObservable.subscribe(stringObserver);
        stringObservable.subscribe(stringSubscriber);
        justObservable.subscribe(onNextAction);
        Observable.just("hello world!").subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                view.append("\nAction1:"+s);
            }
        });
        Observable.just("hello world!").map(new Func1<String, Object>() {
            @Override
            public Object call(String s) {
                return s + "-- hui";
            }
        }).subscribe(s->view.append("\nmap:"+s));
        Observable.just("hello world!").map(s->s.hashCode()).map(s->Integer.toString(s)).subscribe(s->view.append("\nmap2:"+s+"\n"));

        Observable.from(new Object[]{"url1", "url2","url3","url4"}).subscribe(url->view.append("from:"+url));

        Observable.just(new String[]{"url1","url2","url3"}).flatMap(urls->Observable.from(urls)).
                subscribe(url->view.append("\nflatMap:"+url));//just是输入，from是处理变换

        Observable.just(new String[]{"url1","url2","url3"}).flatMap(urls->Observable.from(urls)).
                filter(url->!url.equals("url1")).subscribe(url->view.append("\nfilter:!=url1--"+url));//filter为true就不过滤，为false就过滤

        Observable.just(new String[]{"url1","url2","url3"}).flatMap(urls->Observable.from(urls)).
                filter(url->!url.equals("url1")).take(1).subscribe(url->view.append("\ntake:1--"+url));//限制输入元素个数

        Observable.just(new String[]{"url1","url2","url3"}).flatMap(urls->Observable.from(urls).doOnNext(url->view.append("\ndoOnNext"))).
                filter(url->!url.equals("url1")).subscribe(url->view.append("doOnNext:!=url2-"+url));//添加额外操作，比如保存什么的


    }
}
